/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package id3;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

/**
 *
 * @author SONY VAIO
 */
public class InputFile {

    public static ArrayList<Record> buildRecords(String path) throws IOException {
        ArrayList<Record> records = new ArrayList<Record>();
        FileInputStream fis = new FileInputStream(path);
        DataInputStream dis = new DataInputStream((fis));
        BufferedReader reader = new BufferedReader(new InputStreamReader(dis));

        String line;
        Record r = null;
        int attribute_id = 0;

        ArrayList<Attribute> attributes;

        while ((line = reader.readLine()) != null) {
            attributes = new ArrayList<Attribute>();
            r = new Record();

            StringTokenizer st = new StringTokenizer(line, " ");
            String Token = st.nextToken();
            if (Token.equalsIgnoreCase("@attribute")) {
                String attribute_name = st.nextToken();
                String attribute_value = st.nextToken();
                AttributeDetail attribute_detail = new AttributeDetail(attribute_id, attribute_name, attribute_value);
                // Test attribute detail
                Utility.attributeDetails.add(attribute_detail);
                //attribute_detail.PrintListValue();
                attribute_id++;
            } else {
                StringTokenizer _st = new StringTokenizer(line, ",");
                for (int i = 0; i < Utility.attributeDetails.size(); ++i) {
                    String _nama = Utility.attributeDetails.get(i).nama;
                    attributes.add(new Attribute(_nama, _st.nextToken()));
                }
                r.setAttribute(attributes);
                records.add(r);
            }
        }

        Utility.ATTRIBUTE_NUM = Utility.attributeDetails.size();
        return records;
    }

    public static ArrayList<Record> buildRecordTest(String path) throws IOException {
        ArrayList<Record> records = new ArrayList<Record>();
        FileInputStream fis = new FileInputStream(path);
        DataInputStream dis = new DataInputStream((fis));
        BufferedReader reader = new BufferedReader(new InputStreamReader(dis));

        String line;
        Record r = null;

        ArrayList<Attribute> attributes;
        while ((line = reader.readLine()) != null) {
            attributes = new ArrayList<Attribute>();
            r = new Record();
            StringTokenizer _st = new StringTokenizer(line, ",");
            for (int i = 0; i < Utility.attributeDetails.size()-1; ++i) {
                String _nama = Utility.attributeDetails.get(i).nama;
                attributes.add(new Attribute(_nama, _st.nextToken()));
            }
            r.setAttribute(attributes);
            records.add(r);
        }
        return records;
    }
}
